package com.qy.controller.manage;

import com.github.pagehelper.PageHelper;
import com.qy.base.core.*;
import com.qy.base.utils.DateUtil;
import com.qy.base.utils.kuaizuyunSMS.KuaiZuYunSMS;
import com.qy.model.Member;
import com.qy.model.Message;
import com.qy.model.Withdraw;
import com.qy.service.MemberService;
import com.qy.service.MessageService;
import com.qy.service.WithdrawService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by zaq on 2018/07/10.
 */
@RestController("Manage-WithdrawController")
@RequestMapping("/manage/withdraw")
public class WithdrawController {
    @Resource
    private WithdrawService withdrawService;

    @Resource
    private MemberService memberService;

    @Resource
    private MessageService messageService;

    @PostMapping("/add")
    public Result add(@RequestBody Withdraw withdraw) {
        withdrawService.save(withdraw);
        return ResultGenerator.successResult();
    }

    @PostMapping("/delete")
    public Result delete(@RequestBody Integer id) {
        withdrawService.deleteById(id);
        return ResultGenerator.successResult();
    }

    @PostMapping("/update")
    public Result update(@RequestBody Withdraw withdraw) {
        withdrawService.update(withdraw);
        return ResultGenerator.successResult();
    }

    @GetMapping("/detail")
    public Result detail(@RequestParam Integer id) {
        Withdraw withdraw = withdrawService.findById(id);
        return ResultGenerator.successResult(withdraw);
    }

    @GetMapping("/list")
    public Result list(PageBean<Withdraw> page) {
        PageHelper.startPage(page.getPageNum(), page.getSize());
        List<Withdraw> list = withdrawService.findAll();
        page.setList(list);
        return ResultGenerator.successResult(page);
    }

    //提现管理列表
    @GetMapping("withdrawList")
    public Result withdrawList(PageBean<Map> page, Integer type, String content, Integer withdraw_state) {
        Withdraw withdraw = new Withdraw();
        if (type == 1) {
            withdraw.setPhone(content);
        } else if (type == 2) {
            withdraw.setApplicant(content);
        } else if (type == 3) {
            withdraw.setCard_holder(content);
        }
        withdraw.setWithdraw_state(withdraw_state);
        PageHelper.startPage(page.getPageNum(), page.getSize(), "w.add_time desc");
        List<Map> wiList = withdrawService.withdrawList(withdraw);
        page.setList(wiList);
        return ResultGenerator.successResult(page);
    }

    //提现管理列表-查看
    @GetMapping("/seeWithdraw")
    public Result seeWithdraw(Integer id) {
        List<Map> wiList = withdrawService.seeWithdraw(id);
        return ResultGenerator.successResult(wiList.get(0));
    }

    //提现管理列表-打款
    @PostMapping("/makeMoney")
    public Result makeMoney(@RequestBody Withdraw withdraw) throws Exception {
        if (withdraw.getId() == null) {
            throw new ServiceException(Constants.CODE_ERR_PARAM_ID);
        }
        if (withdraw.getRemitter() == null) {
            throw new ServiceException(Constants.CODE_ERR_PARAM);
        }
        if (withdraw.getWithdraw_state() == null) {
            throw new ServiceException(Constants.CODE_ERR_PARAM);
        }
        withdraw.setRemit_time(String.valueOf(new Date().getTime()));
        withdrawService.makeMoney(withdraw);

        Withdraw withdraw1 = withdrawService.findById(withdraw.getId());
        Member member = memberService.findById(withdraw1.getUser_id());
        //产生消息
        Message message = new Message();
        message.setUser_id(withdraw1.getUser_id());
        message.setMessage_type(1);
        message.setMessage_port(1);
        if (withdraw.getWithdraw_state() == 2) {
            message.setMessage_content("您的提现申请成功，请注意查收");
            Boolean result = KuaiZuYunSMS.sendSMS(member.getPhone(), null, 5);
            if (!result) {
                return ResultGenerator.errResult();
            }

        } else if (withdraw.getWithdraw_state() == 3) {
            message.setMessage_content("您的提现申请不成功，请重新提交");
            Boolean result = KuaiZuYunSMS.sendSMS(member.getPhone(), null, 6);
            member.setBalance(String.valueOf(Double.valueOf(member.getBalance()) + Double.valueOf(withdraw1.getApply_amount())));
            memberService.update(member);
            if (!result) {
                return ResultGenerator.errResult();
            }
        }
        message.setAdd_time(String.valueOf(new Date().getTime()));
        messageService.save(message);

        return ResultGenerator.successResult();
    }
}
